# Maintainer: Alexey Slokva <Alesha72003@ya.ru>

_realname=volk
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.1.2
_cpuver=0.9.0
pkgrel=5
pkgdesc="The Vector Optimized Library of Kernels (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/gnuradio/volk'
msys2_references=(
  'archlinux: libvolk'
)
license=('spdx:LGPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-orc")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-mako"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("https://github.com/gnuradio/volk/releases/download/v${pkgver}/volk-${pkgver}.tar.xz"{,.asc}
        "https://github.com/google/cpu_features/archive/v${_cpuver}/cpufeatures-${_cpuver}.tar.gz"
        0001-volk-remove-full-python-path-shebang.patch
        0002-volk-modtool-remove-py-extension.patch)
sha256sums=('ce62f645c2ee3ef8b38f923de78ef4df9a1b7696d618f57781955803ee29bab3'
            'SKIP'
            'bdb3484de8297c49b59955c3b22dba834401bc2df984ef5cfc17acbe69c5018e'
            'be15ce010b06f18cd7a1c064f91bd7ebef32fc815f358fbac6cacd335c00c44c'
            'e00c539295aa249788350d921ba6246e13c61f34e5c57781ca54cff9abeb810a')
validpgpkeys=('60FD9F5FDCDC9C8CE87BA5150579D69772CD9B22') # Johannes Demel <jdemel@gnuradio.org>

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"
  rm -rf cpu_features
  cp -r "${srcdir}/cpu_features-${_cpuver}" cpu_features

  _apply_patch_with_msg \
    0001-volk-remove-full-python-path-shebang.patch \
    0002-volk-modtool-remove-py-extension.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DVOLK_PYTHON_DIR=" \
    ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DVOLK_PYTHON_DIR=$(cygpath -u $(${MINGW_PREFIX}/bin/python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")) \
    "${extra_config[@]}" \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/cmake --build . --target test || warning "Tests failed"
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
